package com.tjec.project.businessWorkbench.service.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;

import com.tjec.common.utils.ServletUtils;
import com.tjec.common.utils.StringUtils;
import com.tjec.common.utils.file.FileNetUrl;
import com.tjec.common.utils.poi.ImportExcelUtil;
import com.tjec.framework.security.service.TokenService;
import com.tjec.project.system.domain.BmsAttachment;
import com.tjec.project.system.domain.vo.BmsAttachmentVo;
import com.tjec.project.system.mapper.BmsAttachmentMapper;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.tjec.project.businessWorkbench.domain.BmsBusinessDocArchive;
import com.tjec.project.businessWorkbench.domain.BmsBusinessPickup;
import com.tjec.project.businessWorkbench.domain.ImportBmsBusinessTask;
import com.tjec.project.businessWorkbench.domain.ImportBmsProjectDocCatalog;
import com.tjec.project.businessWorkbench.mapper.BmsBusinessDocArchiveMapper;
import com.tjec.project.businessWorkbench.service.IBmsBusinessPickupService;
import com.tjec.project.businessWorkbench.service.IBmsBusinessTaskService;
import com.tjec.project.businessWorkbench.service.IBmsImportExcelService;
import com.tjec.project.businessWorkbench.service.IBmsProjectDocCatalogService;

/**
 * 业务主页导入Service业务层处理
 * 
 * @author pangyongfeng
 * @date 2020-05-12
 */
@Service
public class BmsBusinessServiceImpl implements IBmsImportExcelService {

	@Autowired
	private BmsAttachmentMapper bmsAttachmentMapper;
	@Autowired
	private TokenService tokenService;

	@Autowired
	private IBmsBusinessTaskService bmsBusinessTaskService;

	@Autowired
	private IBmsBusinessPickupService bmsBusinessPickupService;

	@Autowired
	private IBmsProjectDocCatalogService bmsProjectDocCatalogService;

	@Autowired
	private BmsBusinessDocArchiveMapper bmsBusinessDocArchiveMapper;

	@Value("${ftpAddress.url}")
	private String url;

	@Override
	public int importTemplte(String guid, String flag, String businessId) {
		int res = 0;
		if (StringUtils.isNotBlank(guid) && StringUtils.isNotBlank(flag)) {
			

			 List<BmsAttachment> attachments = bmsAttachmentMapper.selectBmsAttachmentByBizId(guid);
			if (CollectionUtils.isEmpty(attachments)) {
				return 0;
			}

			StringBuffer sb = new StringBuffer(url);
			sb.append("/profile");
			sb.append(attachments.get(0).getStoragePath());
			//sb.append("prod-api/profile/template/前期咨询-任务管理清单模板文件.xlsx");
			File file = null;
			if (url.startsWith("https://")) {
				file = FileNetUrl.getNetUrlHttps(sb.toString());
			} else {
				//file = FileNetUrl.getNetUrlHttp("http://103.20.251.85/profile/files/1592743364305/1101-项目建议书编制.xlsx");
				file = FileNetUrl.getNetUrlHttp(sb.toString());
			}
			try {
				FileInputStream inputStream = new FileInputStream(file);
				List<List<Object>> list = ImportExcelUtil.getBankListByExcel(inputStream, "前期咨询-任务管理清单模板文件.xlsx");
				if ("1".equals(flag)) {
					if (list.size() > 0) {
						List<ImportBmsBusinessTask> importBmsBusinessTasks = new ArrayList<ImportBmsBusinessTask>();
						for (int i = 0; i < list.size(); i++) {
							ImportBmsBusinessTask importBmsBusinessTask = new ImportBmsBusinessTask();
							for (int j = 0; j < list.get(i).size(); j++) {
								if (j == 0) {
									importBmsBusinessTask.setFirstName(list.get(i).get(j).toString());
								} else if (j == 1) {
									importBmsBusinessTask.setSecondName(list.get(i).get(j).toString());
								} else if (j == 2) {
									importBmsBusinessTask.setThirdName(list.get(i).get(j).toString());
								} else if (j == 3) {
									if (StringUtils.isBlank(list.get(i).get(j).toString())) {
										importBmsBusinessTask.setMilestoneFlag("0");
									} else {
										if ("是".equals(list.get(i).get(j).toString())) {
											importBmsBusinessTask.setMilestoneFlag("1");
										} else {
											importBmsBusinessTask.setMilestoneFlag("0");
										}
									}

								}

							}
							importBmsBusinessTasks.add(importBmsBusinessTask);
						}
						bmsBusinessTaskService.importBmsBusinessTasks(importBmsBusinessTasks, businessId);
						res = 1;
					}

				} else if ("2".equals(flag)) {
					List<BmsBusinessPickup> pickupList = new ArrayList<BmsBusinessPickup>();
					for (int i = 0; i < list.size(); i++) {
						BmsBusinessPickup bmsBusinessPickup = new BmsBusinessPickup();
						for (int j = 0; j < list.get(i).size(); j++) {
							if (j == 0) {
								bmsBusinessPickup.setName(list.get(i).get(j).toString());
								bmsBusinessPickup.setBusinessId(businessId);
								bmsBusinessPickup.setCreateBy(
										tokenService.getLoginUser(ServletUtils.getRequest()).getUser().getUserId());
							}else if(j == 1) {
								bmsBusinessPickup.setPickUpExplain(list.get(i).get(j).toString());
							}
						}
						if(!"".equals(bmsBusinessPickup.getName()) && bmsBusinessPickup.getName()!=null) {
							pickupList.add(bmsBusinessPickup);
						}
						
					}
					res = bmsBusinessPickupService.importBmsBusinessPickups(pickupList, businessId);
				} else if ("3".equals(flag)) {
					if (list.size() > 0) {
						List<ImportBmsProjectDocCatalog> projectDocCatalogList = new ArrayList<ImportBmsProjectDocCatalog>();
						for (int i = 0; i < list.size(); i++) {
							ImportBmsProjectDocCatalog importBmsProjectDocCatalog = new ImportBmsProjectDocCatalog();
							for (int j = 0; j < list.get(i).size(); j++) {
								if (j == 0) {
									importBmsProjectDocCatalog.setFirstName(list.get(i).get(j).toString());
								} else if (j == 1) {
									importBmsProjectDocCatalog.setSecondName(list.get(i).get(j).toString());
								} else if (j == 2) {
									importBmsProjectDocCatalog.setThirdName(list.get(i).get(j).toString());
								} else if (j == 3) {
									importBmsProjectDocCatalog.setFourthName(list.get(i).get(j).toString());
								} else if (j == 4) {
									importBmsProjectDocCatalog.setFifthName(list.get(i).get(j).toString());
								} else if (j == 5) {
									importBmsProjectDocCatalog.setSixthName(list.get(i).get(j).toString());
								} else if (j == 6) {
									importBmsProjectDocCatalog.setThirdName(list.get(i).get(j).toString());
								}

							}
							
							projectDocCatalogList.add(importBmsProjectDocCatalog);
						}
						bmsProjectDocCatalogService.importBmsProjectDocCatalogs(projectDocCatalogList, businessId);
						res = 1;
					}

				} else if ("4".equals(flag)) {
					List<BmsBusinessDocArchive> docArchiveList = new ArrayList<BmsBusinessDocArchive>();
					for (int i = 0; i < list.size(); i++) {
						BmsBusinessDocArchive bmsBusinessDocArchive = new BmsBusinessDocArchive();
						for (int j = 0; j < list.get(i).size(); j++) {
							if (j == 0) {
								bmsBusinessDocArchive.setName(list.get(i).get(j).toString());
								bmsBusinessDocArchive.setBusinessId(businessId);
								bmsBusinessDocArchive.setCreateBy(
										tokenService.getLoginUser(ServletUtils.getRequest()).getUser().getUserId());
							} else if (j == 1) {
								if (StringUtils.isBlank(list.get(i).get(j).toString())) {
									bmsBusinessDocArchive.setMustFlag("0");
								} else {
									if ("是".equals(list.get(i).get(j).toString())) {
										bmsBusinessDocArchive.setMustFlag("1");
									} else {
										bmsBusinessDocArchive.setMustFlag("0");
									}
								}

							} else if (j == 2) {
								bmsBusinessDocArchive.setShape(list.get(i).get(j)==null?"":list.get(i).get(j).toString());
							}
						}
						if(!"".equals(bmsBusinessDocArchive.getName()) && bmsBusinessDocArchive.getName()!=null) {
							docArchiveList.add(bmsBusinessDocArchive);
						}
						
					}
					res = bmsBusinessDocArchiveMapper.importDocArchivedBatach(docArchiveList);
				}
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
		return res;
	}

}
